package com.sleepfish.web.controller.system;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sleepfish.system.domain.vo.DictDataSimpleRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.sleepfish.common.annotation.Log;
import com.sleepfish.common.core.controller.BaseController;
import com.sleepfish.common.core.domain.AjaxResult;
import com.sleepfish.common.core.domain.entity.SysDictData;
import com.sleepfish.common.core.page.TableDataInfo;
import com.sleepfish.common.enums.BusinessType;
import com.sleepfish.common.utils.StringUtils;
import com.sleepfish.common.utils.poi.ExcelUtil;
import com.sleepfish.system.service.ISysDictDataService;
import com.sleepfish.system.service.ISysDictTypeService;

/**
 * 数据字典信息
 * 
 * @author xiaochao
 */
@Api(tags = "字典数据信息")
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController
{
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @ApiOperation(value = "数据字典列表")
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDictData dictData)
    {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获取部门精简信息列表", notes = "只包含被开启的部门，主要用于前端的下拉选项")
    public AjaxResult getSimpleDepts() {
        // 获得部门列表，只要开启状态的

        List<SysDictData> list = dictDataService.getSimpleDepts();
        List<DictDataSimpleRespVO> simList =new ArrayList<>();
        for (SysDictData sysDictData : list) {
            DictDataSimpleRespVO dictDataSimpleRespVO=new DictDataSimpleRespVO();
            dictDataSimpleRespVO.setDictType(sysDictData.getDictType());
            dictDataSimpleRespVO.setCssClass(sysDictData.getCssClass());
            dictDataSimpleRespVO.setLabel(sysDictData.getDictLabel());
            dictDataSimpleRespVO.setValue(sysDictData.getDictValue());
            dictDataSimpleRespVO.setColorType(sysDictData.getListClass());
            simList.add(dictDataSimpleRespVO);
        }
        return AjaxResult.success(simList);
    }


    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:dict:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictData dictData)
    {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        util.exportExcel(response, list, "字典数据");
    }

    @ApiOperation(value = "查询字典数据详细")
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(
            @ApiParam(name = "dictCode", value = "字典编码", required = true)
            @PathVariable Long dictCode)
    {
        return AjaxResult.success(dictDataService.selectDictDataById(dictCode));
    }


    @ApiOperation(value = "根据字典类型查询字典数据信息")
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(
            @ApiParam(name = "dictType", value = "字典类型", required = true)
            @PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return AjaxResult.success(data);
    }



    @ApiOperation(value = "新增字典类型")
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDictData dict)
    {
        dict.setCreateBy(getUsername());
        return toAjax(dictDataService.insertDictData(dict));
    }

    @ApiOperation(value = "修改保存字典类型")
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDictData dict)
    {
        dict.setUpdateBy(getUsername());
        return toAjax(dictDataService.updateDictData(dict));
    }

    @ApiOperation(value = "删除字典类型")
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public AjaxResult remove(
            @ApiParam(name = "dictCodes", value = "字典编码数组", required = true)
            @PathVariable Long[] dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }


}
